﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ymatou.ServiceCenter
{
    /// <summary>
    /// RPC客户端配置
    /// </summary>
    public class RpcClientConfig
    {
        /// <summary>
        /// RPC客户端默认配置值
        /// </summary>
        public static readonly RpcClientConfig DefaultRpcClientConfig = new RpcClientConfig
        {
            AppId = "Default",
            UpdateMetadataDelaySecond = "UpdateMetadataDelaySecond".GetAppSettingsValue<int>(180),
            EnableUseRpcCall = "EnableUseRpcCall".GetAppSettingsValue<bool>(false),
            EnableSendNullData = "EnableSendNullData".GetAppSettingsValue<bool>(false),
            SendDataDelayMinute = "SendDataDelayMinute".GetAppSettingsValue<int>(30),
            HttpRequestTimeOut = "DefaultTimeOut".GetAppSettingsValue<int>(60000)
        };

        /// <summary>
        /// 应用编号
        /// </summary>
        public string AppId { get; set; }

        /// <summary>
        /// 是否启用RPC客户端（由服务消费者自己在使用ServiceClient.Execute方法时作外部开关判断）
        /// </summary>
        public bool EnableUseRpcCall { get; set; }

        /// <summary>
        /// 如果没有接口调用是否发送RPC采样数据
        /// </summary>
        public bool EnableSendNullData { get; set; }

        /// <summary>
        /// 上传采样数据的时间间隔（单位：分钟）
        /// </summary>
        public int SendDataDelayMinute { get; set; } 

        /// <summary>
        /// 定时获取RPC客户端配置 和 定时获取服务依赖配置 的定时任务的时间间隔（单位：秒）
        /// </summary>
        public int UpdateMetadataDelaySecond { get; set; }

        /// <summary>
        /// Http请求执行的超时时间（单位：毫秒 )
        /// </summary>
        public int HttpRequestTimeOut { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 最后一次更新时间
        /// </summary>
        public DateTime? LastUpdateTime { get; set; }

        /// <summary>
        /// 检查值的范围
        /// </summary>
        public void Check()
        {
            if (SendDataDelayMinute <= 0) SendDataDelayMinute = 30;
            if (UpdateMetadataDelaySecond <= 0) UpdateMetadataDelaySecond = 180;
            if (HttpRequestTimeOut <= 0) HttpRequestTimeOut = 60000;
        }

        public override string ToString()
        {
            return
                string.Format(
                    "AppId:{0},EnableUseRpcCall:{1},EnableSendNullData:{2},SendDataDelayMinute:{3},UpdateMetadataDelaySecond:{4},CreateTime:{5}，HttpRequestTimeOut:{6}",
                    AppId, EnableUseRpcCall, EnableSendNullData, SendDataDelayMinute,
                    UpdateMetadataDelaySecond, CreateTime, HttpRequestTimeOut);
        }

    }
}
